package federationexchange;

import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeoutException;

public class Producer {

    public static final String FED_EXCHANGE = "fed_exchange";

    public static void main(String[] args) throws IOException, TimeoutException {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("192.168.208.134");
        factory.setUsername("admin");
        factory.setPassword("123");
        try (Connection connection = factory.newConnection();
             Channel channel = connection.createChannel()) {
            
            Map<String,Object> arguments = new HashMap<>();
            arguments.put("x-max-priority", 10);
            // 声明优先级队列
            channel.queueDeclare(FED_EXCHANGE, false, false, false, arguments);
            for(int i=1;i<10;i++){
                String publishMessage = "info"+i;
                if(i == 5){
                    AMQP.BasicProperties properties = new AMQP.BasicProperties().builder().priority(5).build();
                    channel.basicPublish("", FED_EXCHANGE, properties,publishMessage.getBytes(StandardCharsets.UTF_8));
                }else {
                    channel.basicPublish("", FED_EXCHANGE, null,publishMessage.getBytes(StandardCharsets.UTF_8));
                }
            }
            System.out.println("消息发送完毕...");
        }
    }
}
